<template>
  <!-- 公共导入组件 -->
  <upload-excel :on-success="success" :before-upload="beforeUpload" />
</template>

<script>
import { importEmployee } from "@/api/employees.js";
export default {
  methods: {
    beforeUpload(val) {
      if (val.size > 1024 * 1024 * 2) {
        this.$message.error("文件不能大于2M");
        return false;
      }
      return true;
    },

    async success({ results }) {
      const userRelations = {
        入职日期: "timeOfEntry",
        手机号: "mobile",
        姓名: "username",
        转正日期: "correctionTime",
        工号: "workNumber",
      };
      let list = results.map((item) => {
        let obj = {};
        for (let key in item) {
          if (key == "入职日期" || key == "转正日期") {
            let date = new Date((item[key] - 1) * 24 * 60 * 60 * 1000);
            let year = date.getFullYear();
            date.setFullYear(year - 70);
            obj[userRelations[key]] = date;
          } else {
            obj[userRelations[key]] = item[key];
          }
        }
        return obj;
      });
      await importEmployee(list);
      this.$router.back();
      this.$message.success("导入成功");
    },
  },
};
</script>

<style>
</style>